define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            Table.api.init({
                extend: {
                    index_url: 'video/index' + location.search,
                    add_url: 'video/add',
                    edit_url: 'video/edit',
                    del_url: 'video/del',
                    multi_url: 'video/multi',
                    table: 'video',
                }
            });

            var table = $("#table");

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'collection.title', title: __('Collection')},
                        {field: 'title', title: __('Title')},
                        {field: 'cover_image', title: __('Cover_image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'duration', title: __('Duration'), formatter: function(value){
                            var hours = Math.floor(value / 3600);
                            var minutes = Math.floor((value % 3600) / 60);
                            var seconds = value % 60;
                            return (hours > 0 ? hours + ':' : '') + 
                                   (minutes < 10 ? '0' + minutes : minutes) + ':' + 
                                   (seconds < 10 ? '0' + seconds : seconds);
                        }},
                        {field: 'play_count', title: __('Play_count')},
                        {field: 'is_charge', title: __('Is_charge'), searchList: {'0':__('Free'),'1':__('Charge')}, formatter: Table.api.formatter.normal},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'status', title: __('Status'), searchList: {'0':__('Hidden'),'1':__('Normal')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});